import { getItem, setItem,removeItem } from '@/utils/storage'
import { LANG, TAGS_VIEW } from '@/constant'


// const currentPath = computed(() => route.path)


export default {
  namespaced: true,
  state: () => ({
    sidebarOpened: true,
    language: getItem(LANG) || 'zh',
    tagsViewList: getItem(TAGS_VIEW) || []
  }),
  mutations: {
    /**
     * 初始化 tagsList
     */
    initTagsViewList(state) {
      state.tagsViewList = []
    },
    /**
     * 添加 tags
     */
    addTagsViewList(state, tag) {
      const isFind = state.tagsViewList.find(item => {
        return item.path === tag.path
      })
      // 处理重复
      if (!isFind) {
        state.tagsViewList.push(tag)
        setItem(TAGS_VIEW, state.tagsViewList)
      }
    },
    triggerSidebarOpened(state){
      state.sidebarOpened= !state.sidebarOpened
    },
    /**
     * 为指定的 tag 修改 title
     */
    changeTagsView(state, { index, tag }) {
      state.tagsViewList[index] = tag
      setItem(TAGS_VIEW, state.tagsViewList)
    },
    /**
     * 删除 tag
     * @param {type: 'other'||'right'||'index', index: index} payload
     */

    removeTagsView(state, payload) {
      if (payload.type === 'index') {
        state.tagsViewList.splice(payload.index, 1)
        setItem(TAGS_VIEW, state.tagsViewList) // 添加持久化
        return
      } else if (payload.type === 'other') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1
        )
        state.tagsViewList.splice(0, payload.index)
        setItem(TAGS_VIEW, state.tagsViewList) // 添加持久化
      } else if (payload.type === 'right') {
        state.tagsViewList.splice(
          payload.index + 1,
          state.tagsViewList.length - payload.index + 1,
        )
        setItem(TAGS_VIEW, state.tagsViewList) // 添加持久化
      } else if (payload.type === 'self') {
        state.tagsViewList.splice(payload.index, 1)
        setItem(TAGS_VIEW, state.tagsViewList) // 添加持久化
        return
      }
    }
    // removeTagsView(state, payload) {
    //   if (payload.type === 'index') {
    //     state.tagsViewList.splice(payload.index, 1)
    //     return
    //   } else if (payload.type === 'other') {
    //     state.tagsViewList.splice(
    //       payload.index + 1,
    //       state.tagsViewList.length - payload.index + 1
    //     )
    //     state.tagsViewList.splice(0, payload.index)
    //   } else if (payload.type === 'right') {
    //     state.tagsViewList.splice(
    //       payload.index + 1,
    //       state.tagsViewList.length - payload.index + 1,
    //     )
    //   }else if(payload.type === 'self'){
    //     state.tagsViewList.splice(payload.index, 1)
    //     // if (state.tagsViewList) {
    //     //   router.push(state.tagsViewList.path)
    //     // } else {
    //     //   // 如果没有其他标签，跳转到首页
    //     //   router.push('/')
    //     // }
    //     return
        
    //             }
    //   // setItem(TAGS_VIEW, state.tagsViewList)
    // }
  },
  actions: {}
}
